/*
 * net_logout.c
 *
 *  Created on: 2023年10月12日
 *      Author: fjx
 */
#include "app_log.h"
#include "softwareMultiTimer.h"
#include "net_logout.h"
#include "net_addressing.h"
#include "device.h"
#include "btl_reset_info.h"
#include "device_nvm.h"

#if defined (RFM_DEVICE)
#define DELAYLOGOUTTIME            (5*60)         //5min

static MultiTimer delayLogoutTimer = {.Handle = 0};


void rfm_formatting(void)
{
  BootloaderResetCause_t* resetCause = (BootloaderResetCause_t*) (SRAM_BASE);
  resetCause->reason = BOOTLOADER_RESET_REASON_FORMATTING;
  resetCause->signature = BOOTLOADER_RESET_SIGNATURE_VALID;

#ifdef CONFIG_NVM_VERSION
  device_rfm_formatting();
#else
  uint8_t buf[16];
  memset(buf,0,16);
  setThisParam(ENpbyRfmId,buf);
  setThisParam(ENpbySystemID,buf);
  setThisParam(ENpbyPanId, buf);
  setThisParam(ENpbyCodeABKey,buf);
#endif

  DS_app_log_error("device formatting  \n");
  NVIC_SystemReset();
}

void rfm_formatting_no_reset(void)
{

#ifdef CONFIG_NVM_VERSION
  device_rfm_formatting();
#else
  uint8_t buf[16];
  memset(buf,0,16);
  setThisParam(ENpbyRfmId,buf);
  setThisParam(ENpbySystemID,buf);
  setThisParam(ENpbyPanId, buf);
  setThisParam(ENpbyCodeABKey,buf);
#endif

  DS_app_log_error("device formatting  \n");

}
static void delayLogoutCallback(MultiTimer* timer, void *userData)
{
  (void) timer;
  (void) userData;
  rfm_formatting();

}

void startDelayLogoutTimer(void)
{
  softwareMultiTimerStart(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &delayLogoutTimer, 32768*DELAYLOGOUTTIME, delayLogoutCallback,NULL,1);
}

bool get_pandid_test(void)
{
  uint32_t panid;
  panid = readThisSystemId();//readThisPanId();
  if(panid == TEST_SYSTEMID){
      return true;
  }
  return false;
}

void check_logout(void)
{
  uint32_t panid;
  panid = readThisSystemId();//readThisPanId();
  if(panid == TEST_SYSTEMID){
      startDelayLogoutTimer();
  }
}

#endif
